<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>【{$name}】</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="format-detection" content="telephone=no" />
<link href="{$Think.config.RESOURCEURL}wx_assets/css/common.css{$Think.config.RESOURCE_VER}" rel="stylesheet" type="text/css">
<link href="{$Think.config.RESOURCEURL}wx_assets/quan/recruit.css{$Think.config.RESOURCE_VER}" rel="stylesheet" type="text/css">
<link href="{$Think.config.RESOURCEURL}wx_assets/quan/bind.css{$Think.config.RESOURCE_VER}" rel="stylesheet" type="text/css">
<script type="text/javascript" src="{$Think.config.RESOURCEURL}wx_assets/js/zepto.1.1.3.min.js"></script>
<script type="text/javascript" src="{$Think.config.RESOURCEURL}wx_assets/js/zepto.alert.js"></script>
<script src="{$Think.config.RESOURCEURL}wx_assets/js/util.js{$Think.config.RESOURCE_VER}" type="text/javascript"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script src="{$Think.config.RESOURCEURL}wx_assets/js/wx_share.js{$Think.config.RESOURCE_VER}" type="text/javascript"></script>
    <style>
        .main .recruit a.disabled{
            color: white;
            background-color: #E6E6E6;
            border: solid 1px #E6E6E6;
        }
    </style>
</head>
<body>
<div class="main">
    <div class="recruit">
        <div class="you_title">
            <img src="{$logo}">
        </div>
        <div class="ym_list binding" id="regBox">
            <dl>
                <dt>
                    <input class="name_s" id="fullName" type="text" name=""
                           value="" placeholder="请输入您的姓名"/>
                </dt>
            </dl>
            <dl>
                <dt>
                    <input class="name_s" id="wxId"
                           value="" placeholder="请确保您的微信号填写无误"/>
                </dt>
            </dl>
            <dl>
                <dt>
                    <input class="name_s" id="mobile" type="tel"
                           value="" placeholder="请输入手机号码"/>
                </dt>
            </dl>
            <a class="save-btn" id="BtnSave">申请加入</a>
            <span class="footText">来，一起做点有意思的事儿！</span>
        </div>
    </div>
</div>
 <script>
    var baseUrl = '{$Think.config.SERVER_HOST}';
    var basePath = 'call.json?customerId={$fromCustomerId}';
    var userName='{$userName}';
	var reg={
        timer:null,
        init:function(){
            this._initValidate();
            this._initExamine();
        },
        _initExamine:function(){
            var examine = '{$examine}';
            if(examine){
                $.util.showMsg("请等待审核通过。");
                $("#BtnSave").addClass("disabled");
                $("#BtnGetCode").addClass("sended");
            }
        },
        _verifyCode:function(){
            var me = this;
            var payJoin = "{$payJoin}";
            var satisfy = "{$satisfy}";
            if(payJoin==1&&satisfy==0){
                me._initBuy();
            }else{
                me._save();
            }
        },
        _save:function(){
            var p=$.util.getParamAsObject();
//            $.util.pageLoading.show("保存中...");
            $.req.get("call.json?module=quan&customerId={$customerId}&method=regQuanSave",{
                userName:"{$userName}",
                params:{
                    "phone":$("#mobile").val(),
                    "fullName":$("#fullName").val(),
                    "wxId":$("#wxId").val(),
                    "friendCount":0,
                    "qId": p.qId||"{$qId}",
                    "tId":"{$tId}",
                    "miUId": p.miUId||"{$miUId}"
                }
            },function(data){
                $.util.pageLoading.hide();
                if(!data.code) {
                    if(p.community){
                        var param={
                            "qId": "{$qId}",
                            "community":1
                        };
                    }else{
                        var param={
                            "qId": "{$qId}"
                        };
                    }
                    location.href = baseUrl+"call.html?customerId={$customerId}&module=quan&method=index&params="+JSON.stringify(param);
                    return;
                } else {
                    $.util.showMsg(data.msg);
                }
            });
        },
        _initValidate:function(){
        	var me = this;
            var config = {};
            config = {
                "mobile": {
                    rule: {
                        required: {
                            message: "请输入11位手机号码！"
                        }
                    },
                    pos: "bottom"
                },
                "wxId":{
                    rule: {
                        required: {
                            message: "请输入微信号"
                        }
                    },
                    pos: "bottom"
                },
                "fullName":{
                	rule: {
                        required: {
                            message: "请输入姓名"
                        }
                    },
                    pos: "bottom"
                }
            }
            var va = new Validate("regBox", config);
            $("#BtnSave").click(function() {
                if($(this).hasClass("disabled")){
                    return;
                }
                va.validate(function(result) {
                    if (result) {
                        $("#BtnSave").addClass("disabled");
                        $.util.pageLoading.show("正在保存...");
                        me._verifyCode();
                    }
                });
            });
        },
        getShareUrl:function(){
            var qId="{$qId}";
            var shareUrl="";
            var params={
                "qId":"{$qId}",
                "fromUId":"{$fromUId}"
            };
            params=encodeURIComponent(JSON.stringify(params));
            shareUrl=baseUrl+'call.html?customerId={$customerId}&module=quan&scope=1&method=mireg&params='+params;
            return shareUrl;
        },
        _initBuy: function() {
            var me = this;
            var p = $.util.getParamAsObject();
            var customerId = $.util.getParam("customerId");
            var uid = null;
            if('{$uid}'!=p.miUId){
                uid = p.miUId;
            }
            var params = {
                "pids": "{$joinPid}",
                "type": "buy",
                "qId": "{$qId}" || "",
                "miUId": uid,
                "vId": "{$vId}"|| "",
                "shareId": "{$sId}",
				"distributionId":'{$distributionId}',
                "counts": 1
            };
            $.req.get(basePath+"&module=product&method=preSubmitOrder", {
                params: params
            }, function (d) {
                if (d.hasOwnProperty('code')) {
                    $.util.alert(d.msg);
                    $.util.pageLoading.hide();
                    $("#BtnSave").removeClass("disabled");
                } else {
                    //调用订单提交
                    var oid = d.id;
                    me.submitOrder(oid, function () {
                        if(p.community){
                            var param={
                                "qId": "{$qId}",
                                "community":1
                            };
                        }else{
                            var param={
                                "qId": "{$qId}"
                            };
                        }
                        location.href = baseUrl+"call.html?customerId={$customerId}&module=quan&method=index&params="+JSON.stringify(param);
                        return;
                    });
                }
            });
        },
        submitOrder: function(oid,callback) {
            var me = this;
            var payType="wxpay";
            var btn = $("#BtnSave");
            var params = {
                "id":oid,
                "remark":"",
                "sid": -1,
                "type":"buy",
                "phone":$("#mobile").val(),
                "fullName":$("#fullName").val(),
                "wxId":$("#wxId").val(),
                "address":"",
                "joinQuan":1,
                "paytype": payType,
                "dtype": "delivery"
            };
            $.req.get(basePath+"&module=product&method=submitOrder", {
                params: params
            }, function (data) {
                if (typeof(data.code) !== "undefined") {
                    if(data.code==2){
                        $.util.pageLoading.hide();
                        if(p.community){
                            var param={
                                "qId": "{$qId}",
                                "community":1
                            };
                        }else{
                            var param={
                                "qId": "{$qId}"
                            };
                        }
                        location.href = baseUrl+"call.html?customerId={$customerId}&module=quan&method=index&params="+JSON.stringify(param);
                        return;
                    }
                    $.util.alert(data.msg);
                    return;
                }
                if (payType == "wxpay") {
                    try {
                        var price = data.price;
                        WeixinJSBridge.invoke('getBrandWCPayRequest', data.pkg, function(res) {
                            if (res.err_msg == 'get_brand_wcpay_request:ok') {
                                callback && callback();
//                                me.sendNotice(oid,callback);
                            } else {
                                me.cancelOrder(oid,function() {
                                    if (res.err_msg == 'get_brand_wcpay_request:cancel') {
                                        return $.util.alert("操作已取消");
                                    } else if (res.err_msg == 'get_brand_wcpay_request:fail') {
                                        return $.util.alert("网络错误发送失败，请稍后再试");
                                    } else if (res.err_msg == 'access_control:not_allow' || res.err_msg == 'system:access_denied') {
                                        return $.util.alert("商家尚未开通微信支付");
                                    } else {
                                        return $.util.alert(res.err_msg);
                                    }
                                });
                            }
                        });
                    } catch (e) {
                        me.cancelOrder(oid,function() {
                            $(this).removeClass("disabled");
                            return $.util.alert("操作失败，请确保在微信浏览器中操作");
                        });
                    }
                }  else {
                    $(this).removeClass("disabled");
                    $.util.alert("支付方式错误");
                }
            },function(){
                $(this).removeClass("disabled");
                $.util.alert("购买失败");
            });
        },
        sendNotice:function(id,callback){
            var pa = {
                "id": id,
                "tplid": 1
            };
            $.ajax({
                url: window.basePath,
                dataType: "json",
                type: "POST",
                data: {
                    module: 'product',
                    method: 'sendNotice',
                    params: JSON.stringify(pa)
                },
                success: function() {
                    callback && callback();
                }
            });
        }
    }
    reg.init();
    var shareUrl=reg.getShareUrl();
    wx_share.init({
            "title":"警示！所有号称一夜暴富的都是耍流氓",
            "desc":'我是{$nickName},我从不相信一夜暴富，踏踏实实赚钱，才是正道。',
            "link":shareUrl,
            "imgUrl":"{$headImgUrl}",
            "pkg":{
                "appId": '{$pkg.appId}',
                "timestamp": {$pkg.timestamp},
                "nonceStr": '{$pkg.nonceStr}',
                "signature": '{$pkg.signature}'
            },
            success:function(config,shareType){
         }
    });
</script>
</body>
</html>